import {formatRouterRequireComponent} from "../../utils/formate";
import Components from '../../config/components';
import {MenuInterface} from "../interface/MenuInterface";
import {MENU} from "../types";

const initMenuState: MenuInterface = {
    menu: [], // 菜单数据
    breadcrumbs: [], // 当前的面包屑
    menuList: [],
    menuMapping: {},
}
const menuReducer = (state: any = initMenuState, action: any) => {
    if (!action.payload) return state;
    switch (action.type) {
        case MENU.MENU_LIST:
            let {list} = action.payload;
            list = list.sort((a: any, b: any) => b.orderNum - a.orderNum);
            const menuMergeCompData = formatRouterRequireComponent(list, Components);
            return Object.assign({}, state, {menu: menuMergeCompData});
        case MENU.BREADCRUMBS:
            return Object.assign({}, state, {breadcrumbs: action.payload});
        default:
            return state;

    }

}
export default menuReducer;
